home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 7 / Example 7.4 / mesh.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-01  |  4.9 KB  |  198 lines

  1. #include "mesh.h"
  2.  
  3. const DWORD ObjectVertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
  4.  
  5. MESH::MESH()
  6. {
  7.     m_pDevice = NULL;
  8.     m_pMesh = NULL;
  9. }
  10.  
  11. MESH::MESH(char fName[], IDirect3DDevice9* Dev)
  12. {
  13.     m_pDevice = Dev;
  14.     m_pMesh = NULL;
  15.     Load(fName, m_pDevice);
  16. }
  17.  
  18. MESH::~MESH()
  19. {
  20.     Release();
  21. }
  22.  
  23. HRESULT MESH::Load(char fName[], IDirect3DDevice9* Dev)
  24. {
  25.     m_pDevice = Dev;
  26.  
  27.     //Set m_white material
  28.     m_white.Ambient = m_white.Specular = m_white.Diffuse  = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  29.     m_white.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
  30.     m_white.Power = 1.0f;
  31.  
  32.     Release();
  33.  
  34.     //Load new m_pMesh
  35.     ID3DXBuffer * adjacencyBfr = NULL;
  36.     ID3DXBuffer * materialBfr = NULL;
  37.     DWORD noMaterials = NULL;
  38.  
  39.     if(FAILED(D3DXLoadMeshFromX(fName, D3DXMESH_MANAGED, m_pDevice,    &adjacencyBfr, &materialBfr, NULL, &noMaterials, &m_pMesh)))
  40.         return E_FAIL;
  41.  
  42.     D3DXMATERIAL *mtrls = (D3DXMATERIAL*)materialBfr->GetBufferPointer();
  43.  
  44.     for(int i=0;i<noMaterials;i++)
  45.     {
  46.         m_materials.push_back(mtrls[i].MatD3D);
  47.  
  48.         if(mtrls[i].pTextureFilename != NULL)
  49.         {
  50.             char textureFileName[90];
  51.             strcpy(textureFileName, "meshes/");
  52.             strcat(textureFileName, mtrls[i].pTextureFilename);
  53.             IDirect3DTexture9 * newTexture = NULL;
  54.             D3DXCreateTextureFromFile(m_pDevice, textureFileName, &newTexture);            
  55.             m_textures.push_back(newTexture);
  56.         }
  57.         else m_textures.push_back(NULL);
  58.     }
  59.  
  60.     m_pMesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE,
  61.                             (DWORD*)adjacencyBfr->GetBufferPointer(), NULL, NULL, NULL);
  62.  
  63.     adjacencyBfr->Release();
  64.     materialBfr->Release();
  65.  
  66.     return S_OK;
  67. }
  68.  
  69. void MESH::Render()
  70. {
  71.     for(int i=0;i<m_materials.size();i++)
  72.     {    
  73.         if(m_textures[i] != NULL)m_pDevice->SetMaterial(&m_white);
  74.         else m_pDevice->SetMaterial(&m_materials[i]);
  75.         m_pDevice->SetTexture(0,m_textures[i]);
  76.         m_pMesh->DrawSubset(i);
  77.     }    
  78. }
  79.  
  80. void MESH::Release()
  81. {
  82.     //Clear old mesh...
  83.     if(m_pMesh != NULL)
  84.     {
  85.         m_pMesh->Release();
  86.         m_pMesh = NULL;
  87.     }
  88.  
  89.     //Clear textures and materials
  90.     for(int i=0;i<m_textures.size();i++)
  91.         if(m_textures[i] != NULL)
  92.             m_textures[i]->Release();
  93.  
  94.     m_textures.clear();
  95.     m_materials.clear();    
  96. }
  97.  
  98. MESHINSTANCE::MESHINSTANCE()
  99. {
  100.     m_pMesh = NULL;
  101.     m_pos = m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  102.     m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
  103. }
  104.  
  105. MESHINSTANCE::MESHINSTANCE(MESH *meshPtr)
  106. {
  107.     m_pMesh = meshPtr;
  108.     m_pos = m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  109.     m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
  110. }
  111.  
  112. D3DXMATRIX MESHINSTANCE::GetWorldMatrix()
  113. {
  114.     D3DXMATRIX p, r, s;
  115.     D3DXMatrixTranslation(&p, m_pos.x, m_pos.y, m_pos.z);
  116.     D3DXMatrixRotationYawPitchRoll(&r, m_rot.y, m_rot.x, m_rot.z);
  117.     D3DXMatrixScaling(&s, m_sca.x, m_sca.y, m_sca.z);
  118.  
  119.     D3DXMATRIX world = s * r * p;
  120.     return world;
  121. }
  122.  
  123. void MESHINSTANCE::Render()
  124. {
  125.     if(m_pMesh != NULL)
  126.     {
  127.         m_pMesh->m_pDevice->SetTransform(D3DTS_WORLD, &GetWorldMatrix());
  128.         m_pMesh->Render();
  129.     }
  130. }
  131.  
  132. BBOX MESHINSTANCE::GetBoundingBox()
  133. {
  134.     if(m_pMesh == NULL || m_pMesh->m_pMesh == NULL)return BBOX();
  135.     
  136.     if(m_pMesh->m_pMesh->GetFVF() != ObjectVertex::FVF)        // XYZ and NORMAL and UV
  137.         return BBOX();
  138.  
  139.     BBOX bBox(D3DXVECTOR3(-10000.0f, -10000.0f, -10000.0f),
  140.               D3DXVECTOR3(10000.0f, 10000.0f, 10000.0f));
  141.     D3DXMATRIX World = GetWorldMatrix();
  142.  
  143.     //Lock vertex buffer of the object
  144.     ObjectVertex* vertexBuffer = NULL;
  145.     m_pMesh->m_pMesh->LockVertexBuffer(0,(void**)&vertexBuffer);
  146.  
  147.     //For each vertex in the m_pMesh
  148.     for(int i=0;i<m_pMesh->m_pMesh->GetNumVertices();i++)
  149.     {
  150.         //Transform vertex to world space using the MESHINSTANCE
  151.         //world matrix, i.e. the position, rotation and scale
  152.         D3DXVECTOR3 pos;
  153.         D3DXVec3TransformCoord(&pos, &vertexBuffer[i]._pos, &World);
  154.  
  155.         // Check if the vertex is outside the bounds
  156.         // if so, then update the bounding volume
  157.         if(pos.x < bBox.min.x)bBox.min.x = pos.x;
  158.         if(pos.x > bBox.max.x)bBox.max.x = pos.x;
  159.         if(pos.y < bBox.min.y)bBox.min.y = pos.y;
  160.         if(pos.y > bBox.max.y)bBox.max.y = pos.y;
  161.         if(pos.z < bBox.min.z)bBox.min.z = pos.z;
  162.         if(pos.z > bBox.max.z)bBox.max.z = pos.z;
  163.     }
  164.  
  165.     m_pMesh->m_pMesh->UnlockVertexBuffer();
  166.  
  167.     return bBox;
  168. }
  169.  
  170. BSPHERE MESHINSTANCE::GetBoundingSphere()
  171. {
  172.     if(m_pMesh == NULL || m_pMesh->m_pMesh == NULL)return BSPHERE();
  173.     if(m_pMesh->m_pMesh->GetFVF() != ObjectVertex::FVF)        // XYZ and NORMAL and UV
  174.         return BSPHERE();
  175.  
  176.     BBOX bBox = GetBoundingBox();
  177.     BSPHERE bSphere;
  178.     D3DXMATRIX World = GetWorldMatrix();
  179.     bSphere.center = (bBox.max + bBox.min) / 2.0f;
  180.  
  181.     ObjectVertex* vertexBuffer = NULL;
  182.     m_pMesh->m_pMesh->LockVertexBuffer(0,(void**)&vertexBuffer);
  183.  
  184.     //Get radius
  185.     for(int i=0;i<m_pMesh->m_pMesh->GetNumVertices();i++)
  186.     {
  187.         D3DXVECTOR3 pos;
  188.         D3DXVec3TransformCoord(&pos, &vertexBuffer[i]._pos, &World);
  189.  
  190.         float l = D3DXVec3Length(&(pos - bSphere.center));
  191.         if(l > bSphere.radius)
  192.             bSphere.radius = l;
  193.     }
  194.  
  195.     m_pMesh->m_pMesh->UnlockVertexBuffer();
  196.  
  197.     return bSphere;
  198. }